package org;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//import java.io.FileInputStream;
//import java.net.URL;



import org.entities.LevelObject;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Color;
import org.newdawn.slick.Input;
import org.newdawn.slick.Image;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.util.ResourceLoader;
import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Font;

public class InfoState  extends BasicGameState {

    private StateBasedGame game;
    private Image logo,titlebg,screen;
    private boolean up,down;
    private Font font;
    private String line;
    private int textScroll;
    private int currentPressTime = 0;    
    private int maxPressTime = 100;      
    private boolean nextPressOK;   
    public static final int ID = 10;    
    public int getID() {
        return ID;
    }

    public void init(GameContainer container, StateBasedGame game) throws SlickException {

        this.game = game;		
        titlebg = new Image("data/image/titlebg.jpg");
        logo = new Image("data/image/sweaselLogo.png");
        font = new AngelCodeFont("data/font/weasel.fnt","data/font/weasel.png");
        screen = new Image("data/image/ctrlScreen3.png");


        StringBuilder sb=new StringBuilder();
        try {
            BufferedReader reader=new BufferedReader(
                    new InputStreamReader(ResourceLoader.getResourceAsStream("README.txt")));
            String linex="";
            while ((linex = reader.readLine()) != null) {
                sb.append(linex);
                sb.append('\n');
            }
            reader.close();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        line=sb.toString();
    }

    public void update(GameContainer container, StateBasedGame game, int delta){

        if (currentPressTime < maxPressTime) {
            currentPressTime += delta;
            nextPressOK = false;
        }
        if (currentPressTime >= maxPressTime) {
            nextPressOK = true;
            currentPressTime =0;
        }
        if (nextPressOK){
            move();
        }
    }

    private void move(){
        if (up&&!(textScroll>=0)) {
            textScroll+=16;
        }   
        if (down&&!(textScroll<=-3072)) {
            textScroll-=16;
            //System.out.printf("textScroll=[%d]\n",textScroll);
        } 
    }

    public void render(GameContainer container, StateBasedGame game, Graphics g) {
        if (!LevelObject.debugMode)
            g.scale(2f, 2f);
        g.drawImage(titlebg,0,0);
        g.drawImage(logo,200-(logo.getWidth()/2),50-(logo.getHeight()/2));
        g.drawImage(screen, 88, 112);
        //g.resetTransform(); 

        g.scale(0.8f, 0.8f);
        g.setColor(Color.white);
        g.setFont(font);        
        g.drawString(Progress.version, 420, 5);
        //g.resetTransform(); 

        g.scale(0.5f, 0.5f);
        g.setClip(186,240,428,254);
        g.drawString(line, 250, 300+textScroll);
        g.resetTransform(); 
        g.clearClip();
    }


    public void keyPressed(int key, char c) {

        if (key == Input.KEY_DOWN) {
            down=true;
        }
        if (key == Input.KEY_UP) {
            up=true;
        }
    }    

    public void keyReleased(int key, char c) {

        if (key == Input.KEY_DOWN) {
            down=false;
        }
        if (key == Input.KEY_UP) {
            up=false;
        }        
        if (key == Input.KEY_ESCAPE) {			
            game.enterState(TitleState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));			
        }
    }
}
